var mysql = require('mysql');

var pool = mysql.createPool({
	host : "localhost",
	port : 3306,
	user : "updios",
	password : "1q2w3e??",
	database : "stock"
});

exports.list = function(req, res){

	//데이타베이스 쿼리를 실행합니다.
	pool.getConnection(function(err, connection) {
		// Use the connection
		connection.query( 'SELECT * FROM products', function(err, rows) {
			// 응답합니다.
			res.render('product/List',{data:rows});
		});
		connection.release();
	});
};


exports.del = function(req, res) {
	console.log("com");
	//데이타베이스 쿼리를 실행합니다.
	pool.getConnection(function(err, connection) {

		// Use the connection
		connection.query( 'DELETE FROM products WHERE id = ? ', [req.param('id')]) ; 
		connection.release();

		// 응답합니다.
		res.writeHead(302, {'Location':'/list'});
		res.end();
	});
};


exports.insForm = function(req, res) {
	res.render('product/Insert');
};


exports.insert = function(req, res) {
	pool.getConnection(function(err, connection) {

		// Use the connection
		connection.query( 'INSERT INTO products(name, modelnumber, series) VALUES ( ?,?,?)', [req.param('name'),req.param('modelnumber'),req.param('series')]) ; 
		connection.release();

		// 응답합니다.
		res.writeHead(302, {'Location':'/list'});
		res.end();
	});
};

exports.editForm = function(req,res) {
	pool.getConnection(function(err, connection) {

		  // Use the connection
		connection.query( 'SELECT * FROM products WHERE id = ?', [req.params.id], function(err, rows) {
			// 응답합니다.
			
			res.render('product/Edit',{data:rows[0]});
			connection.release();
		});
	});
};

exports.edit = function(req, res) {
	// Use the connection
	pool.getConnection(function(err, connection) {

		// Use the connection
		connection.query( 'UPDATE products SET name = ?, modelnumber = ?, series = ? WHERE id = ?', [req.param('name'), req.param('modelnumber'), req.param('series'),req.param('id')]) ; 
		connection.release();

		// 응답합니다.
		res.writeHead(302, {'Location':'/list'});
		res.end();
	});
};


/*
// GET - /EDIT/:id
app.get('/Edit/:id', function(request, response) {
	// List.htm 파일을 읽습니다.
	fs.readFile('Edit.htm','utf8',function(error,data){
		//데이타베이스 쿼리를 실행합니다.
		pool.getConnection(function(err, connection) {

			  // Use the connection
			connection.query( 'SELECT * FROM products WHERE id = ?', [request.params.id], function(err, rows) {
				// 응답합니다.
				response.writeHead(200, {'Content-Type':'text/html'});
				response.end(ejs.render(data,{
					data:rows[0]
				}));
				connection.release();
			});
		});
	});
});


// POST - /Edit
app.post('/Edit/:id', function(request, response) {
	// 변수를 선언합니다.
	var body = request.body;
	// Use the connection
	pool.getConnection(function(err, connection) {

		// Use the connection
		connection.query( 'UPDATE products SET name = ?, modelnumber = ?, series = ? WHERE id = ?', [body.name, body.modelnumber, body.series, request.params.id]) ; 
		connection.release();

		// 응답합니다.
		response.writeHead(302, {'Location':'/'});
		response.end();
	});

});

 */